#include <vector>
#include <algorithm>
#include <string>
#include <climits>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <iostream>
#include <unordered_map>
#include <cstring>
#include <iomanip>
#include <array>
#include <set>
#include <iterator>
using namespace std;
typedef long long ll;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
#define F first;
#define S second;
ll mod = 1e9 + 7;
ll inf = 1e18;
const int MAX_N = 1e5 + 1;
typedef struct tri {
ll a;
ll b;
ll c;
}tri;
ll gcd(ll x, ll y) {
if (y > x) {
ll z = y;
y = x;
x = z;
}
ll ans = y;
while (x % y != 0) {
ans = x % y;
x = y;
y = ans;
}
return ans;
}
ll binexp(ll n, ll p) {
ll ans = 1;
while (p) {
if (p % 2) {
ans = (ans * n) % mod;
}
n = (n * n) % mod;
p /= 2;
}
return ans;
}
bool f(ll a, ll b) {
return a > b;
}
void solve() {
ll n; cin >> n;
vector<ll>a(n), b(n);
ll ans = 0, sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
ans += a[i] * a[i];
sum += a[i];
}
for (int j = 0; j < n; j++) {
cin >> b[j];
ans += b[j] * b[j];
sum += b[j];
}
ans *= (n - 2);
vector<vector<ll>>dp(n, vector<ll>(1e4 + 1));
dp[0][a[0]] = 1;
dp[0][b[0]] = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j <= 1e4; j++) {
if (j >= a[i]) dp[i][j] = dp[i][j] || dp[i - 1][j - a[i]];
if (j >= b[i]) dp[i][j] = dp[i][j] || dp[i - 1][j - b[i]];
}
}
ll mn = LLONG_MAX;
for (int j = 0; j <= 1e4; j++) {
if (!dp[n - 1][j]) continue;
mn = min(mn, j * j + (sum - j) * (sum - j));
}
cout << ans + mn << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t; cin >> t;
while (t--) {
solve();
}
return 0;
}
41C - Email address | 1373D - Maximum Sum on Even Positions |
1574C - Slay the Dragon | 621A - Wet Shark and Odd and Even |
1395A - Boboniu Likes to Color Balls | 1637C - Andrew and Stones |
1334B - Middle Class | 260C - Balls and Boxes |
1554A - Cherry | 11B - Jumping Jack |
716A - Crazy Computer | 644A - Parliament of Berland |
1657C - Bracket Sequence Deletion | 1657B - XY Sequence |
1009A - Game Shopping | 1657A - Integer Moves |
230B - T-primes | 630A - Again Twenty Five |
1234D - Distinct Characters Queries | 1183A - Nearest Interesting Number |
1009E - Intercity Travelling | 1637B - MEX and Array |
224A - Parallelepiped | 964A - Splits |
1615A - Closing The Gap | 4C - Registration System |
1321A - Contest for Robots | 1451A - Subtract or Divide |
1B - Spreadsheet | 1177A - Digits Sequence (Easy Edition) |